import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_locations/test_page.dart';

import 'locale_helper.dart';
import 'localizations.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  // This widget is the root of your application.
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {

  SpecificLocalizationDelegate _specificLocalizationDelegate;

  @override
  void initState() {
    super.initState();
    helper.onLocaleChanged = onLocaleChange;
    _specificLocalizationDelegate = SpecificLocalizationDelegate(new Locale("en"));
  }

   onLocaleChange(Locale locale){
    setState((){
      _specificLocalizationDelegate = new SpecificLocalizationDelegate(locale);
    });
  }
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        title: 'Flutter Demo',
        localizationsDelegates: [
          GlobalMaterialLocalizations.delegate,
          GlobalWidgetsLocalizations.delegate,
          FallbackCupertinoLocalisationsDelegate(),
          _specificLocalizationDelegate,
        ],
        supportedLocales: [Locale("en"), Locale("zh")],
        locale: _specificLocalizationDelegate.overriddenLocale,
        theme: ThemeData(
          primarySwatch: Colors.blue,
        ),
        home: TestPage(),
    );
  }
}

